// 题目描述
// https://leetcode.cn/problems/search-in-a-sorted-array-of-unknown-size/

/**
 * // This is the ArrayReader's API interface.
 * // You should not implement it, or speculate about its implementation
 * function ArrayReader() {
 *
 *     @param {number} index
 *     @return {number}
 *     this.get = function(index) {
 *         ...
 *     };
 * };
 */

/**
 * @param {ArrayReader} reader
 * @param {number} target
 * @return {number}
 */
var search = function (reader, target) {
  let left = 0
  let right = Number.MAX_VALUE
  while (left <= right) {
    const mid = left + Math.floor((right - left) / 2)
    const res = reader.get(mid)
    if (res === 2 ** 23 - 1) {
      right = mid - 1
    } else if (res === target) {
      return mid
    } else if (res < target) {
      left = mid + 1
    } else if (res > target) {
      right = mid - 1
    }
  }

  return -1
}
